package com.example.administrator.reader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class Zhihuadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    OnItemClickListener onItemClickListener;
    List<Integer> list;
    private List<ZhiHu> zhiHulist;
    private Context context;
    BaseSliderView.OnSliderClickListener onSliderClickListener;
    public String detail_URL = "https://news-at.zhihu.com/api/4/news/";
    public String url = "";

    private Animation animation;

    public interface OnItemClickListener {
        Void onitemclickview(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void net() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("zjs", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Log.d("url", res);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(res);
                    MainActivity.share_url = jsonObject.getString("share_url");
                    Log.d("url", MainActivity.share_url);
                    Intent intent = new Intent(context, Main2Activity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);


                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }


        });
    }


    public Zhihuadapter(List<ZhiHu> zhiHulist, final Context context) {
        this.zhiHulist = zhiHulist;
        this.context = context;
        final MainActivity mainActivity = new MainActivity();
        list = mainActivity.getDate_postion();
        onSliderClickListener = new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                url = detail_URL + slider.getBundle().get("id");
                net();


            }
        };

    }

    @Override
    public int getItemViewType(int position) {
        Log.d("laile", "getItemViewType:1 ");

        if (position == 0) {
            Log.d("laile", "getItemViewType: ");
            return 0;
        }
        for (int i = 0; i < list.size(); i++) {
            Log.d("sssss", list.get(i) + " ");
            if (position == list.get(i)) {
                return 2;
            }
        }

        return 1;


    }

    @SuppressLint("ResourceType")
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == 2) {
            return new ViewHolder2(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.bigzhihu, viewGroup, false));
        } else if (i == 1) {
            return new ViewHolder1(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.littlezhizhu, viewGroup, false));
        } else {
            return new ViewHolder0(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mheaderview, viewGroup, false));

        }


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (getItemViewType(i) == 0) {
            final ViewHolder0 viewHolder0 = (ViewHolder0) viewHolder;
            for (int j = 0; j < MainActivity.imageUrls.size(); j++) {
                TextSliderView tsv = new TextSliderView(context);
                tsv.image(MainActivity.imageUrls.get(j)).description(MainActivity.descriptions.get(j));
                tsv.bundle(new Bundle());
                tsv.getBundle().putString("id", MainActivity.itemid.get(j));
                tsv.setOnSliderClickListener(onSliderClickListener);
                viewHolder0.sliderShow.addSlider(tsv);
            }
            viewHolder0.sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);//使用默认指示器，在底部显示
            viewHolder0.sliderShow.setDuration(2000);//停留时间
            viewHolder0.sliderShow.setPresetTransformer(SliderLayout.Transformer.Default);
        } else {
            ZhiHu zhiHu = zhiHulist.get(i - 1);
            Log.d("now", MainActivity.date + "3");
            if (onItemClickListener != null) {
                if (getItemViewType(i) == 2) {
                    final ViewHolder2 viewHolder2 = (ViewHolder2) viewHolder;
                    Glide.with(context).load(zhiHu.getImageurl()).into(viewHolder2.imageView);
                    viewHolder2.textView.setText(zhiHu.getTitle());
                    viewHolder2.date.setText(MainActivity.hashMap.get(i));
                    Log.d("0427", MainActivity.hashMap.get(i));
                    viewHolder2.linearLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = viewHolder2.getLayoutPosition();
                            onItemClickListener.onitemclickview(v, pos - 1);
                        }
                    });

                } else if (getItemViewType(i) == 1) {
                    final ViewHolder1 viewHolder1 = (ViewHolder1) viewHolder;
                    Glide.with(context).load(zhiHu.getImageurl()).into(viewHolder1.imageView);
                    viewHolder1.textView.setText(zhiHu.getTitle());
                    viewHolder1.item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = viewHolder1.getLayoutPosition();
                            onItemClickListener.onitemclickview(v, pos - 1);
                        }
                    });
                }
            }

        }

    }

    @Override
    public int getItemCount() {
        return zhiHulist.size() + 1;
    }


    class ViewHolder0 extends RecyclerView.ViewHolder {

        SliderLayout sliderShow;

        public ViewHolder0(@NonNull View itemView) {
            super(itemView);
            sliderShow = itemView.findViewById(R.id.slider);

        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        View item;

        public ViewHolder1(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.title);
            item = itemView;
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;
        TextView date;
        LinearLayout linearLayout;

        public ViewHolder2(@NonNull View itemView) {
            super(itemView);

            linearLayout = itemView.findViewById(R.id.little);
            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.title);
            date = itemView.findViewById(R.id.date);
        }
    }

}
